#!/usr/bin/perl -w

my $nrow = 0;
my $size = -1;
my $reject = 0;

my @HPI = ();

while(<>) {
    chomp;
    my (@value) = split /\,+/;
    my $nval = @value;
    if ( $size < $nval ) {
	$size = $nval;
    } elsif ( $size > $nval ) {
	++$reject;
	next;
    }
    # header
    if ( $. == 1 ) {
	for ( my $i=1; $i < 4; ++$i ) {
	    $value[$i] =~ s/\"//g;
	    if ( $i == 1 ) {
		print "$value[$i]";
	    } else {
		print " $value[$i]";
	    }
	}
	print "\n";
	next;
    }
    # remove the first column
    # for ( my $i=2; $i < $nval; ++$i ) {
    for ( my $i=1; $i < 4; ++$i ) {
	$HPI[$nrow][$i-1] = $value[$i];
    }
    ++$nrow;
}
print STDERR "$nrow lines processed, $reject rejected.\n";

my @GR = ();
# calculate growth rates
for ( my $r = 1; $r < $nrow; ++$r ) {
    for ( my $i=0; $i < 3; ++$i ) {
	my $gr = log($HPI[$r][$i]/$HPI[$r-1][$i]);
	$GR[$r-1][$i] = $gr;
    }
}

my @GRD = ();
for ( my $r = 1; $r < $nrow-1; ++$r ) {
    for ( my $i=0; $i < 3; ++$i ) {
	my $gr = $GR[$r][$i] - $GR[$r-1][$i];
	$GRD[$r-1][$i] = $gr;
    }
}

for ( my $r = 1; $r < $nrow-2; ++$r ) {
    for ( my $i=0; $i < 3; ++$i ) {
	my $gr = $GRD[$r][$i] - $GRD[$r-1][$i];
	if ( $i == 0 ) {
	    print "$gr";
	} else {
	    print " $gr";
	}
    }
    print "\n";
}

exit(0);
